package com.example.riceexambackend.controller;
import com.example.riceexambackend.controller.param.exam.ExamAttendParam;
import com.example.riceexambackend.controller.param.exam.ExamAutoParam;
import com.example.riceexambackend.controller.param.exam.ExamSubmitParam;
import com.example.riceexambackend.controller.result.exam.*;
import com.example.riceexambackend.dao.dataobject.UserDO;

import com.example.riceexambackend.common.errorcode.ControllerErrorCodeConstants;
import com.example.riceexambackend.common.exception.ControllerException;
import com.example.riceexambackend.common.pojo.CommonResult;
import com.example.riceexambackend.common.utils.JacksonUtil;
import com.example.riceexambackend.controller.param.exam.ExamAddParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeAddParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeDeleteParam;
import com.example.riceexambackend.controller.param.questiontype.QuestionTypeUpdateParam;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeAddResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeDeleteResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeSelectResult;
import com.example.riceexambackend.controller.result.questiontype.QuestionTypeUpdateResult;
import com.example.riceexambackend.service.ExamService;
import com.example.riceexambackend.service.QuestionTypeService;
import com.example.riceexambackend.service.dto.exam.*;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeAddDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeDeleteDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeSelectDTO;
import com.example.riceexambackend.service.dto.questiontype.QuestionTypeUpdateDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/exam")
public class ExamController {
    @Resource
    private ExamService examService;
    
    private static final Logger logger = LoggerFactory.getLogger(ExamController.class);

    @PostMapping("/submit")
    public CommonResult<ExamSubmitResult> submitExam(@Validated @RequestBody ExamSubmitParam examSubmitParam) {
        // 日志打印
        logger.info("ExamController submitExam:{}", JacksonUtil.writeValueAsString(examSubmitParam));
        // 调用提交试卷业务逻辑
        ExamSubmitDTO examSubmitDTO = examService.submitExam(examSubmitParam);
        ExamSubmitResult result = convertToExamSubmitResult(examSubmitDTO);
        return CommonResult.success(result);
    }

    private ExamSubmitResult convertToExamSubmitResult(ExamSubmitDTO examSubmitDTO) {
        if (examSubmitDTO == null) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_SUBMIT_ERROR);
        }
        ExamSubmitResult examSubmitResult = new ExamSubmitResult();
        examSubmitResult.setSuccess(examSubmitDTO.getSuccess());
        return examSubmitResult;
    }

    @PostMapping("/attend")
    public CommonResult<List<ExamAttendResult>> attendExam(@Validated @RequestBody ExamAttendParam examAttendParam) {
        // 日志打印
        logger.info("ExamController attendExam:{}", JacksonUtil.writeValueAsString(examAttendParam));
        // 调用参加考试业务逻辑
        List<ExamAttendDTO> examAttendDTOList = examService.attendExam(examAttendParam);
        List<ExamAttendResult> resultList = convertToExamAttendResultList(examAttendDTOList);
        return CommonResult.success(resultList);
    }

    private List<ExamAttendResult> convertToExamAttendResultList(List<ExamAttendDTO> examAttendDTOList) {
        if (examAttendDTOList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_ATTEND_ERROR);
        }
        List<ExamAttendResult> examAttendResultList = new ArrayList<>();
        for (ExamAttendDTO examAttendDTO : examAttendDTOList) {
            ExamAttendResult examAttendResult = new ExamAttendResult();
            examAttendResult.setQuestionId(examAttendDTO.getQuestionId());
            examAttendResult.setQuestionTypeId(examAttendDTO.getQuestionTypeId());
            examAttendResult.setQuestionName(examAttendDTO.getQuestionName());
            examAttendResult.setOptionA(examAttendDTO.getOptionA());
            examAttendResult.setOptionB(examAttendDTO.getOptionB());
            examAttendResult.setOptionC(examAttendDTO.getOptionC());
            examAttendResult.setOptionD(examAttendDTO.getOptionD());
            examAttendResultList.add(examAttendResult);
        }
        return examAttendResultList;
    }

    @GetMapping("/view/{examId}")
    public CommonResult<List<ExamViewResult>> viewExam(@PathVariable("examId") String examId ) {
        // 日志打印
        logger.info("ExamController viewExam:{}", examId);
        // 调用查看试卷业务逻辑
        List<ExamViewDTO> examViewDTOList = examService.viewExam(examId);
        List<ExamViewResult> resultList = convertToExamViewResultList(examViewDTOList);
        return CommonResult.success(resultList);
    }

    private List<ExamViewResult> convertToExamViewResultList(List<ExamViewDTO> examViewDTOList) {
        if (examViewDTOList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_VIEW_ERROR);
        }
        List<ExamViewResult> examViewResultList = new ArrayList<>();
        for (ExamViewDTO examViewDTO : examViewDTOList) {
            ExamViewResult examViewResult = new ExamViewResult();
            examViewResult.setQuestionId(examViewDTO.getQuestionId());
            examViewResult.setQuestionName(examViewDTO.getQuestionName());
            examViewResult.setOptionA(examViewDTO.getOptionA());
            examViewResult.setOptionB(examViewDTO.getOptionB());
            examViewResult.setOptionC(examViewDTO.getOptionC());
            examViewResult.setOptionD(examViewDTO.getOptionD());
            examViewResult.setRightAnswer(examViewDTO.getRightAnswer());
            examViewResult.setQuestionTypeName(examViewDTO.getQuestionTypeName());
            examViewResultList.add(examViewResult);
        }
        return examViewResultList;
    }

    @PostMapping("/auto")
    public CommonResult<ExamAutoResult> autoGenerateExamQuestion(@Validated @RequestBody ExamAutoParam examAutoParam) {
        // 日志打印
        logger.info("ExamController autoGenerateExamQuestion:{}", JacksonUtil.writeValueAsString(examAutoParam));
        // 调用自动组卷业务逻辑
        ExamAutoDTO examAutoDTO = examService.autoGenerateExamQuestion(examAutoParam);
        ExamAutoResult result = convertToExamDTOResult(examAutoDTO);
        return CommonResult.success(result);
    }

    private ExamAutoResult convertToExamDTOResult(ExamAutoDTO examAutoDTO) {
        if (examAutoDTO == null ||
                examAutoDTO.getSuccess() == null) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_AUTO_ERROR);
        }
        ExamAutoResult examAutoResult = new ExamAutoResult();
        examAutoResult.setSuccess(examAutoDTO.getSuccess());
        return examAutoResult;
    }

    @GetMapping("/select")
    public CommonResult<List<ExamSelectResult>> selectAllExam() {
        // 日志打印
        logger.info("ExamController selectAllExam");
        // 调用查询全部题型业务逻辑
        List<ExamSelectDTO> examSelectDTOList = examService.selectAllExam();
        List<ExamSelectResult> resultList = convertToExamSelectResultList(examSelectDTOList);
        return CommonResult.success(resultList);
    }

    private List<ExamSelectResult> convertToExamSelectResultList(List<ExamSelectDTO> examSelectDTOList) {
        if (examSelectDTOList == null) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_SELECT_ALL_ERROR);
        }
        List<ExamSelectResult> examSelectResultList = new ArrayList<>();
        for (ExamSelectDTO examSelectDTO : examSelectDTOList) {
            ExamSelectResult examSelectResult = new ExamSelectResult();
            examSelectResult.setExamId(examSelectDTO.getExamId());
            examSelectResult.setExamName(examSelectDTO.getExamName());
            examSelectResult.setExamStartTime(examSelectDTO.getExamStartTime());
            examSelectResult.setExamEndTime(examSelectDTO.getExamEndTime());
            examSelectResult.setExamDuration(examSelectDTO.getExamDuration());
            examSelectResult.setExamTotalScore(examSelectDTO.getExamTotalScore());
            examSelectResult.setExamTotalCount(examSelectDTO.getExamTotalCount());
            examSelectResult.setExamUser(examSelectDTO.getExamUser());
            examSelectResult.setExamStatus(examSelectDTO.getExamStatus());
            examSelectResultList.add(examSelectResult);
        }
        return examSelectResultList;
    }
//
//    @PostMapping("/delete")
//    public CommonResult<QuestionTypeDeleteResult> deleteQuestionType(@Validated @RequestBody QuestionTypeDeleteParam param) {
//        // 日志打印
//        logger.info("QuestionTypeController deleteQuestionType:{}", JacksonUtil.writeValueAsString(param));
//        // 调用删除题型业务逻辑
//        QuestionTypeDeleteDTO questionTypeDeleteDTO = questionTypeService.deleteQuestionType(param);
//        QuestionTypeDeleteResult result = convertToQuestionTypeDeleteResult(questionTypeDeleteDTO);
//        return CommonResult.success(result);
//    }
//
//    private QuestionTypeDeleteResult convertToQuestionTypeDeleteResult(QuestionTypeDeleteDTO questionTypeDeleteDTO) {
//        if (questionTypeDeleteDTO == null ||
//            questionTypeDeleteDTO.getQuestionTypeId() <= 0) {
//            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_DELETE_ERROR);
//        }
//        QuestionTypeDeleteResult questionTypeDeleteResult = new QuestionTypeDeleteResult();
//        questionTypeDeleteResult.setQuestionTypeId(questionTypeDeleteDTO.getQuestionTypeId());
//        return questionTypeDeleteResult;
//    }
//
//
//    @PostMapping("/update")
//    public CommonResult<QuestionTypeUpdateResult> updateQuestionType(@Validated @RequestBody QuestionTypeUpdateParam param) {
//        // 日志打印
//        logger.info("QuestionTypeController updateQuestionType:{}", JacksonUtil.writeValueAsString(param));
//        // 调用修改题型业务逻辑
//        QuestionTypeUpdateDTO questionTypeUpdateDTO = questionTypeService.updateQuestionType(param);
//        QuestionTypeUpdateResult result = convertToQuestionTypeUpdateResult(questionTypeUpdateDTO);
//        return CommonResult.success(result);
//    }
//
//    private QuestionTypeUpdateResult convertToQuestionTypeUpdateResult(QuestionTypeUpdateDTO questionTypeUpdateDTO) {
//        if (questionTypeUpdateDTO == null ||
//        questionTypeUpdateDTO.getQuestionTypeId() <= 0) {
//            throw new ControllerException(ControllerErrorCodeConstants.QUESTION_TYPE_UPDATE_ERROR);
//        }
//        QuestionTypeUpdateResult questionTypeUpdateResult = new QuestionTypeUpdateResult();
//        questionTypeUpdateResult.setQuestionTypeId(questionTypeUpdateDTO.getQuestionTypeId());
//        return questionTypeUpdateResult;
//    }

    @PostMapping("/add")
    public CommonResult<ExamAddResult> addExam(@Validated @RequestBody ExamAddParam param) {
        // 日志打印
        logger.info("ExamController addExam:"); // TODO：序列化异常
        // 调用新增试卷业务逻辑
        ExamAddDTO examAddDTO = examService.insertExam(param);
        ExamAddResult result = convertToExamAddResult(examAddDTO);
        return CommonResult.success(result);
    }

    private ExamAddResult convertToExamAddResult(ExamAddDTO examAddDTO) {
        if (examAddDTO == null ||
            examAddDTO.getExamId() <= 0) {
            throw new ControllerException(ControllerErrorCodeConstants.EXAM_ADD_ERROR);
        }
        ExamAddResult examAddResult = new ExamAddResult();
        examAddResult.setExamId(examAddDTO.getExamId());
        return examAddResult;
    }

}
